﻿using System;
using System.Linq;
using System.Threading;
using System.Web.Mvc;
using System.Web.Routing;
using FikriKibar.Common.Entities;
using FikriKibar.Globalization;

namespace FikriKibar.UI.WebCommon.Infrastructure
{
    [AttributeUsage(AttributeTargets.Method)]
    public class FikriKibarSecurityFilter : ActionFilterAttribute
    {
        private int[] Roles { get; }
        public FikriKibarSecurityFilter(params int[] roles)
        {
            Roles = roles;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var fikriKibarIdentity = (FikriKibarIdentity)Thread.CurrentPrincipal.Identity;
            if (!fikriKibarIdentity.IsAuthenticated)
            {
                if (fikriKibarIdentity.UserId > 0)
                {
                    var routeValueDictionary = new RouteValueDictionary
                    {
                        {"culture", fikriKibarIdentity.LanguageCode},
                        {"controller", "Redirection"},
                        {"action", "RedirectAction"},
                        {"message", Resources.NotPermission},
                        {"cssClass", "alert alert-danger"},
                        {"timeout", 2},
                        {"url", "/" + fikriKibarIdentity.LanguageCode + "/User/Login"}
                    };
                    filterContext.Result = new RedirectToRouteResult(routeValueDictionary);
                }
                else
                {
                    var routeValueDictionary = new RouteValueDictionary
                    {
                        {"culture", fikriKibarIdentity.LanguageCode},
                        {"controller", "User"},
                        {"action", "Login"}
                    };
                    filterContext.Result = new RedirectToRouteResult(routeValueDictionary);
                }
            }
            base.OnActionExecuting(filterContext);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var fikriKibarIdentity = (FikriKibarIdentity)Thread.CurrentPrincipal.Identity;
            var isAuthenticated = false;
            if (fikriKibarIdentity.IsAuthenticated)
            {
                if (Roles.Length > 0)
                {
                    if (fikriKibarIdentity.UserRoles.Any(userRole => Roles.Contains(userRole)))
                    {
                        isAuthenticated = true;
                    }
                }
                else
                {
                    isAuthenticated = true;
                }
            }
            if (!isAuthenticated)
            {
                filterContext.Result = new RedirectToRouteResult(
                   new RouteValueDictionary
                   {
                       { "culture", fikriKibarIdentity.LanguageCode},
                       { "controller", "Redirection"},
                       { "action", "RedirectAction"},
                       { "message", Resources.NotPermission},
                       { "cssClass", "alert alert-danger"},
                       { "timeout", 2},
                       { "url", "/" + fikriKibarIdentity.LanguageCode + "/User/Login"}
                   });
            }
            base.OnActionExecuted(filterContext);
        }
    }
}
